#!/usr/bin/python
# .+
#
# .identifier :	$Id: toggle.py,v 0.1 2006/02/23 14:40:45 fabrizio Exp $
# .context    : portio python extension
# .title      : portio demo program
# .kind	      : python source
# .author     : Fabrizio Pollastri <pollastri@iriti.cnr.it>
# .site	      : Torino - Italy
# .creation   :	16-Feb-2006
# .copyright  :	(c) 2006 Fabrizio Pollastri
# .license    : GNU General Public License (see .copying below)
#
# .copying
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
# .-

"""
PortIO demo program
===================

Continuosly toggle on and off all data lines of both parallel ports of
a Linux 386 platform. Toggle cycle is 6 seconds long.
"""
import sys, time, os
import portio

def setup():
   # check for root privileges
  if os.getuid():
    print 'You need to be root! Exiting.'
    sys.exit()

  # acquire permission for I/O on lp0
  portio.iopl(3)
  portio.ioperm(0x378, 1, 1)
  #ioperm arguments are (port, extent, enable) extent sets the number of bytes

def is_box_open():
  setup()
  status = portio.inb(0x379)
  # 0 for desktop, 1 for laptop
  if sys.argv[1] == str(0):
#    print status
    return status == 7
  else:
#    print status
    return status == 0

def open_box():
  setup()
  # toggle for ever the data lines of lp0
  data = 1
  portio.outb(data, 0x378) #data byte
  time.sleep(3)
  data = 0
  portio.outb(data, 0x378)
  
def toggle():
  setup()
  # toggle for ever the data lines of lp0
  data = 0
  portio.outb(data,0x378) #data byte
  portio.outb(data,0x37a) #control byte
  portio.outb(data,0x379) #status byte

  while 1:
  #  lp0in = portio.inb(0x378)
    control = portio.inb(0x37a)
    status = portio.inb(0x379)
  #  portio.out1(data,0x378) 
#    print 'read %x from lp0, written %x to lp0' % (control,status)
  #  data = ~data & 0x1
    time.sleep(3)

#### end
